package com.example.word.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.dict.R;
import com.example.word.bean.User;
import com.example.word.util.HttpUtils;
import com.example.word.util.MessageUtils;
import com.example.word.util.Response;
import com.example.word.util.SPUtils;
import com.example.word.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人信息
 */
public class PersonActivity extends AppCompatActivity {
    private Activity mActivity;
    private ActionBar mTitleBar;//标题栏
    private TextView tvAccount;
    private TextView etNickName;
    private TextView etAge;
    private Button btnSave;//保存
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        mActivity = this;
        tvAccount = findViewById(R.id.tv_account);
        etNickName = findViewById(R.id.tv_nickName);
        etAge = findViewById(R.id.tv_age);
        btnSave = findViewById(R.id.btn_save);
        mTitleBar = (ActionBar) findViewById(R.id.myActionBar);
        mTitleBar.setData(mActivity,"个人信息", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        // 设置主题
        if (!com.example.word.ui.activity.MainActivity.colorChange) {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill));
        } else {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.myColor));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill_my_color));
        }
        initView();
    }

    private void initView() {
        String account = (String) SPUtils.get(mActivity,"account","");
        // 获取当前用户信息
        User user = DataSupport.where("account = ?", account).findFirst(User.class);
        if (user != null) {
            tvAccount.setText(user.getAccount());
            etNickName.setText(user.getNickName());
            etAge.setText(user.getPhoneNumber());
        }
//        Map<String, String> requestParam = new HashMap<>();
//        requestParam.put("account", account);
//        HttpUtils.sendGetRequest("/getUserByAccount", requestParam, User.class, new HttpUtils.ApiCallback<User>() {
//            @Override
//            public void onSuccess(Response<User> response) {
//                // 处理请求成功的情况
//                User user = response.getData();
//                // 跳转页面
//                tvAccount.setText(user.getAccount());
//                etNickName.setText(user.getNickName());
//                etAge.setText(String.valueOf(user.getPhoneNumber()));
//            }
//
//            @Override
//            public void onFailure(Response response) {
//                // 处理请求失败的情况
//                MessageUtils.show(response.getMsg(), mActivity);
//            }
//        });
        //保存
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = tvAccount.getText().toString();
                String nickName = etNickName.getText().toString();
                String phoneNumber = etAge.getText().toString();
                User user1 = DataSupport.where("account = ?",account).findFirst(User.class);
                if ("".equals(nickName)) {
                    MessageUtils.show("昵称不能为空", mActivity);
                    return;
                }
                if ("".equals(phoneNumber)) {
                    MessageUtils.show("电话不能为空", mActivity);
                    return;
                }
                user1.setNickName(nickName);
                user1.setPhoneNumber(String.valueOf(phoneNumber));
                user1.save();
                MessageUtils.show("保存成功！", mActivity);
                finish();//关闭页面
            }
        });

    }
}
